<?php

function getArrUrl($var) {
    $nvar = Array();
    $na = explode("/", $var);
    for ($i = 0; $i < count($na) - 1; $i+=2) {
        $nvar["$na[$i]"] = $na[$i + 1];
    }
    return $nvar;
}

if (isset($_GET['args']))
    $args = getArrUrl($_GET['args']);

function cutString($str, $len) {
    $mystr = "";
    if (strlen($str) > $len) {
        $newstr = substr($str, 0, $len);
        $newstr1 = substr($str, 0, $len + 1);
        $strch = substr($newstr, -1);
        $strch1 = substr($newstr1, -1);
        if ($strch == " " || $strch1 == " ")
            $mystr = $newstr . " ...";
        else
            $mystr = cutString($newstr, $len - 1);
    } else
        $mystr = $str;

    return $mystr;
}

function mysearch($mystrsearch, $findstr) {
    $hlsearch = "<span class=resultsearch>";
    $hlsearchd = "</span>";
    $len_find = strlen($findstr);
    $t = strpos(strtoupper($mystrsearch), strtoupper($findstr));
    $findstrnew = substr($mystrsearch, $t, $len_find);
    while ($t > 0) {
        $friststr = substr($mystrsearch, 0, $t);
        $mystrsearch = substr($mystrsearch, $t + $len_find);
        $myresultstr .= $friststr . $hlsearch . $findstrnew . $hlsearchd;
        $t = strpos(strtoupper($mystrsearch), strtoupper($findstr));
    }
    $myresultstr = $myresultstr . $mystrsearch;
    return $myresultstr;
}

function writefile($File, $Data) {
    $Handle = fopen($File, 'w+');
    fwrite($Handle, $Data);
    fclose($Handle);
}

function myurl($str) {

    $chars = array(
        'a' => array('ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'ắ', 'ằ', 'ẳ', 'ẵ', 'ặ', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ', 'Ặ', 'á', 'à', 'ả', 'ã', 'ạ', 'â', 'ă', 'Á', 'À', 'Ả', 'Ã', 'Ạ', 'Â', 'Ă'),
        'e' => array('ế', 'ề', 'ể', 'ễ', 'ệ', 'Ế', 'Ề', 'Ể', 'Ễ', 'Ệ', 'é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'É', 'È', 'Ẻ', 'Ẽ', 'Ẹ', 'Ê'),
        'i' => array('í', 'ì', 'ỉ', 'ĩ', 'ị', 'Í', 'Ì', 'Ỉ', 'Ĩ', 'Ị'),
        'o' => array('ố', 'ồ', 'ổ', 'ỗ', 'ộ', 'Ố', 'Ồ', 'Ổ', 'Ô', 'Ộ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'Ớ', 'Ờ', 'Ở', 'Ỡ', 'Ợ', 'ó', 'ò', 'ỏ', 'õ', 'ọ', 'ô', 'ơ', 'Ó', 'Ò', 'Ỏ', 'Õ', 'Ọ', 'Ô', 'Ơ'),
        'u' => array('ứ', 'ừ', 'ử', 'ữ', 'ự', 'Ứ', 'Ừ', 'Ử', 'Ữ', 'Ự', 'ú', 'ù', 'ủ', 'ũ', 'ụ', 'ư', 'Ú', 'Ù', 'Ủ', 'Ũ', 'Ụ', 'Ư'),
        'y' => array('ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'Ý', 'Ỳ', 'Ỷ', 'Ỹ', 'Ỵ'),
        'd' => array('đ', 'Đ'),
        '' => array('?', '!', '&'),
    );



    foreach ($chars as $key => $arr)
        foreach ($arr as $val)
            $str = str_replace($val, $key, $str);

    //$str = strtolower(str_replace( array('&nbsp;', '\'', '/', '"', ' - ', ':', '.', '!', '@', '#', '$', '%', '&', '*'), '', $str));
    $str = strtolower(str_replace(array('&nbsp;', '\'', '/', '"', ' - ', ':', '.', '!', '@', '#', '$', '%', '&', '*', '`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '=', '|', '\\', "'", '"', ':', ';', '.', ',', '?', '>', '<', '/', '®', '™', 'é', 'è', 'â', 'à', 'ê', 'û', 'ë', 'ç', 'ô', "’", '“', '”'), '', $str));

    $str = trim($str);

    $str = strtolower(str_replace(' ', '-', $str));

    return $str;
}

function listCat($id, $level, $maxlevel , $table = "product_cat", $mod="product_item") {
    $db = new MySQL();

    $query = "select * from $table where parentid='$id' and level <= $maxlevel order by orderId asc";

    $sql = $db->select($query);
    $i=0;
    while ($row = $db->fetch($sql)) {
        $linkphoto = '<a href="?mod='.$mod.'&act=list&idcat=' . $row["id"] . '">List Items</a>';
        $linkedit = '<a href="javascript:edit(\'' . $row["id"] . '\')">Edit</a> | <a href="javascript:del(\'' . $row["id"] . '\')">Delete</a> ';

        if ($row['level'] == 2) {
            $linkmain = '<input name="txtId[]" type="hidden" value="' . $row["id"] . '" /><input name="txtHome[]" type="checkbox" value="' . $row["id"] . '"';
            if ($row["mainhome"] == 1) {
                $linkmain .= " checked ";
            }
            $linkmain .= ' />';
        } else
            $linkmain = "";

        if ($i % 2 == 1)
            $listitem = "griditem1";
        else
            $listitem = "griditem2";



        $list .='<li class="' . $listitem . '" onMouseOver="mOvr(this, \'bgover\')" onMouseOut="mOut(this, \'' . $listitem . '\')" id="pictureId_' . $row['id'] . '">
					  <div class="divcheck"><input type="checkbox" name="chkid" value="' . $row["id"] . '" /></div>
					  <div class="divtitle" style="padding-left:' . (40 * $level + 2) . 'px" >' . $row["title"] . '</div>					  
					  <div class="divaction">' . $linkedit . '</div>					  
					  <div class="divmore_1">' . $linkphoto . '</div>
					  <!-- <div class="divmore_2">' . $linkmain . '</div> -->
					  <div class="clr"></div>
					</li>	';

        $list .= listCat($row["id"], $row['level'], $maxlevel);
        $i++;
    }
    return $list;
}

/**
 * Get Youtube video ID from URL
 *
 * @param string $url
 * @return mixed Youtube video ID or FALSE if not found
 */
function getYoutubeIdFromUrl($url) {
    $parts = parse_url($url);
    if (isset($parts['query'])) {
        parse_str($parts['query'], $qs);
        if (isset($qs['v'])) {
            return $qs['v'];
        } else if (isset($qs['vi'])) {
            return $qs['vi'];
        }
    }
    if (isset($parts['path'])) {
        $path = explode('/', trim($parts['path'], '/'));
        return $path[count($path) - 1];
    }
    return false;
}

function formatPrice($price) {
    return number_format($price, 0, ',', '.') . ' VNĐ';
}

function __($text = "") {
    if (isset($_SESSION['lang'])) {
        $lang = $_SESSION['lang'];
    } else {
        $lang = 'vn';
    }
    $translate = array(
        "Download" => array(
            "en" => "Download",
            "vn" => "Tải xuống"
        ),
        "Xem chi tiết" => array(
            "en" => "more ...",
            "vn" => "Xem thêm ..."
        ),
        "VN" => array(
            "en" => "EN",
            "vn" => "VN"
        ),
        "Search" => array(
            "en" => "Search",
            "vn" => "Tìm kiếm"
        ),
        "About us" => array(
            "en" => "About us",
            "vn" => "Giới thiệu"
        ),
    );
    if (isset($translate[$text])) {
        echo $translate[$text][$lang];
    } else {
        echo $text;
    }
}

?>